package LeetCode热题HOT100;

public class p213打家劫舍II {
    public int rob(int[] nums) {
        if (nums.length==1) {
            return nums[0];
        }
        if (nums.length==2) {
            return Math.max(nums[0],nums[1]);
        }

        return Math.max(robHelper(nums,0,nums.length-1),robHelper(nums,1,nums.length));
    }

    private int robHelper(int[] nums, int start, int end) {
        int frist=0,second=0,thrid=0;
        for (int i = start; i <end; i++) {
            frist=second;
            second=Math.max(frist,thrid+nums[i]);
            thrid=frist;
        }
        return second;
    }
}
